<template>
	<view>
		<view class="top-timer">
			<view class="left-week-box" @tap="switchTab();">
				<view :class="'month item ' + (tabcur==0?'active':'')">月</view>
				<view :class="'week item ' + (tabcur==1?'active':'')">周</view>
			</view>
			<view class="right-timer-text">
				<view class="left-arrow-img arrow-img" @click="play('jian')">
					<image src="/static/images/arrow-left.png" ></image>
				</view>
				<view class="center-text">
					{{monthweekshow}}
				</view>
				<view class="right-arrow-img arrow-img" @click="play('jia')">
					<image src="/static/images/arrow-right.png"></image>
				</view>
			</view>
		</view>
		<view class="sign-detail">
			<view class="sign-title-box">
				<view class="sign-title-box-content">
					<view class="left-box">
						<view class="avator-box">
							<image :src="userInfo.avatarUrl?'userInfo.avatarUrl':'/static/images/header.png'"></image>
						</view>
						<view class="userinfo-box">
							<view class="name">{{userInfo.nickName}}</view>
							<view class="zhiwei">
								{{userInfo.deptName}}
							</view>
						</view>
					</view>
					<view class="right-box" @click="skipToCalendar">
						<view class="yueli-img">
							<image src="/static/images/yueli-coin.png"></image>
						</view>
						<view class="text">签到月历</view>
					</view>
				</view>
				<view class="public-line-height"></view>
			</view>
			<view class="sign-content">
				<view class="sign-content-item">
					<view class="sign-content-item-box">
						<view class="left-text">
							签到次数
						</view>
						<view class="right-text">
							{{checkInNum}}
						</view>
					</view>
					<view class="public-line-height"></view>
				</view>
				<view class="sign-content-item">
					<view class="sign-content-item-box">
						<view class="left-text">
							签到天数
						</view>
						<view class="right-text">
							{{checkInDays}}
						</view>
					</view>
					<view class="public-line-height"></view>
				</view>
				<view class="sign-content-item">
					<view class="sign-content-item-box">
						<view class="left-text">
							未签到天数
						</view>
						<view class="right-text">
							{{unCheckInDays}}
						</view>
					</view>
					<view class="public-line-height"></view>
				</view>
				<view class="sign-content-item">
					<view class="sign-content-item-box">
						<view class="left-text">
							开户数
						</view>
						<view class="right-text">
							{{openNums}}
						</view>
					</view>
					<view class="public-line-height"></view>
				</view>
				<view class="sign-content-item">
					<view class="sign-content-item-box">
						<view class="left-text">
							未开户数
						</view>
						<view class="right-text">
							{{unOpenNums}}
						</view>
					</view>
					<view class="public-line-height"></view>
				</view>
			</view>
		</view>


		<!-- 周日期选择 -->
		<!-- <xweek v-on:back="changeBack"></xweek> -->

	</view>
</template>

<script>
	import avatar from "../../components/yq-avatar/yq-avatar.vue";
	// import xweek from "../../components/junkj-xweek/xweek.vue";

	const util = require("../../utils/util.js");
	//获取应用实例
	const api = require("../../config/api.js");
	export default {
		data() {
			return {
				tabcur: 0,
				num:0,
				startStopTime:'',
				weektypeArray:[],
				monthweekshow:'',
				
				userId:'',
				
				// 查询签到信息
				userInfo:{
					avatarUrl:"",
					nickName:'',
					deptName:''
					
				},
			
				checkInNum:0,
				checkInDays:0,
				unCheckInDays:0,
				openNums:0,
				unOpenNums:0
				
				
			}
		},
		components: {
			avatar,
			// xweek
		},
		filters: {
			capitalize: function(value) {
				if (!value) return ''
				value = value.toString()
				return value.charAt(0).toUpperCase() + value.slice(1)
			},

			formatTimeMin: function(a) {
				if (!a) return '- - -'
				var myDate = new Date(parseInt(a));
				var datestr = "";
				datestr = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
				datestr = datestr + "-" + ((myDate.getMonth() + 1) < 10 ? "0" + (myDate.getMonth() + 1) : (myDate.getMonth() + 1)); //获取当前月份(0-11,0代表1月
				datestr = datestr + "-" + (myDate.getDate() < 10 ? "0" + myDate.getDate() : myDate.getDate()); //获取当前日(1-31)
				datestr = datestr + " " + (myDate.getHours() < 10 ? "0" + myDate.getHours() : myDate.getHours()); //获取当前时
				datestr = datestr + ":" + (myDate.getMinutes() < 10 ? "0" + myDate.getMinutes() : myDate.getMinutes()); //获取当前分
				return datestr;
			},
			formatTimeOnlyMin: function(a) {
				if (!a) return '- - -'
				var myDate = new Date(parseInt(a));
				var datestr = "";
				// datestr = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
				// datestr =  datestr + "-"+ ((myDate.getMonth() + 1) < 10 ? "0" + (myDate.getMonth() + 1) : (myDate.getMonth() + 1)); //获取当前月份(0-11,0代表1月
				// datestr = datestr + "-" + (myDate.getDate() < 10 ? "0" + myDate.getDate() : myDate.getDate()); //获取当前日(1-31)
				datestr = (myDate.getHours() < 10 ? "0" + myDate.getHours() : myDate.getHours()); //获取当前时
				datestr = datestr + ":" + (myDate.getMinutes() < 10 ? "0" + myDate.getMinutes() : myDate.getMinutes()); //获取当前分
				return datestr;
			}
		},
		onLoad(option) {
			if(option&&option.userId){
				this.userId = option.userId;
			}
			var _that = this;
			var  num  = this.num;
			if(this.tabcur==1){
				// 当前统计月的签到
				_that.getWeekStartAndEnd(num)
			}else{
				// 当前统计周的签到
				
				_that.getMonthStartAndEnd(num)
			}
		
			uni.setStorageSync('isGetShopInforStorage', false);
		},
		methods: {
			skipToCalendar(){
				
				uni.navigateTo({
					url:'/pages/signinMonth/signinMonth?userId=' +this.userId,
				})
				console.log(1111)
			},

			switchTab() {
				var _that = this;
				// 切换月周时把num置为0
				this.num = 0;
				if (this.tabcur == 0) {
					this.tabcur = 1
				} else {
					this.tabcur = 0
				}
				if(this.tabcur==1){
					// 当前统计月的签到
					_that.getWeekStartAndEnd(this.num)
				}else{
					// 当前统计周的签到
					_that.getMonthStartAndEnd(this.num)
				}
			},

			play(type) {
				var _that = this;
				if (type == 'jian') {
					this.num--;
				} else {
					this.num++
				}
				console.log('num', this.num);
				if(this.num>=1){
					uni.showToast({
						title:'不能选择未来日期！',
						icon:'none'
					})
					return 
				}
				if(this.tabcur==1){
					// 当前统计月的签到
					_that.getWeekStartAndEnd(this.num)
				}else{
					// 当前统计周的签到
					_that.getMonthStartAndEnd(this.num)
				}
			},

			// getWeekStartAndEnd(0)
			 getWeekStartAndEnd(AddWeekCount) {
				 console.log("执行周");
				//起止日期数组  
				var startStop = new Array();
				//一天的毫秒数  
				var millisecond = 1000 * 60 * 60 * 24;
				//获取当前时间  
				var currentDate = new Date();
				//相对于当前日期AddWeekCount个周的日期
				currentDate = new Date(currentDate.getTime() + (millisecond * 7 * AddWeekCount));
				//返回date是一周中的某一天
				var week = currentDate.getDay();
				//返回date是一个月中的某一天  
				var month = currentDate.getDate();
				//减去的天数  
				var minusDay = week != 0 ? week - 1 : 6;
				//获得当前周的第一天  
				var currentWeekFirstDay = new Date(currentDate.getTime() - (millisecond * minusDay));
				//获得当前周的最后一天
				var currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + (millisecond * 6));
				//添加至数组  
				var  getDateStr3 = this.getDateStr3;
				startStop.push(getDateStr3(currentWeekFirstDay, 'start'));
				startStop.push(getDateStr3(currentWeekLastDay, 'end'));
				console.log('startStop周', startStop);
				this.startStopTime = startStop;
				
				// 请求接口 
				this.checkStatistics(startStop)
				return startStop;
			},
			/** 
			 * 获得相对当月AddMonthCount个月的起止日期 
			 * AddMonthCount为0 代表当月 为-1代表上一个月 为1代表下一个月 以此类推
			 * ***/
			getMonthStartAndEnd(AddMonthCount) {
				 console.log("执行月");
				//起止日期数组  
				var startStop = new Array();
				//获取当前时间  
				var currentDate = new Date();
				var month = currentDate.getMonth() + AddMonthCount;
				if (month < 0) {
					var n = parseInt((-month) / 12);
					month += n * 12;
					currentDate.setFullYear(currentDate.getFullYear() - n);
				}
				currentDate = new Date(currentDate.setMonth(month));
				//获得当前月份0-11  
				var currentMonth = currentDate.getMonth();
				//获得当前年份4位年  
				var currentYear = currentDate.getFullYear();
				//获得上一个月的第一天  
				var currentMonthFirstDay = new Date(currentYear, currentMonth, 1);
				//获得上一月的最后一天  
				var currentMonthLastDay = new Date(currentYear, currentMonth + 1, 0);
				//添加至数组  
				var  getDateStr3 = this.getDateStr3;
				startStop.push(getDateStr3(currentMonthFirstDay, 'start'));
				startStop.push(getDateStr3(currentMonthLastDay, 'end'));
				console.log('startStop 月', startStop);
				this.startStopTime = startStop;
				// 请求接口
				this.checkStatistics(startStop)
				//返回  
				return startStop;
			},


			//获取当前日期yy-mm-dd
			//date 为时间对象
			getDateStr3(date, endstart) {
				var year = "";
				var month = "";
				var day = "";
				var now = date;
				year = "" + now.getFullYear();
				if ((now.getMonth() + 1) < 10) {
					month = "0" + (now.getMonth() + 1);
				} else {
					month = "" + (now.getMonth() + 1);
				}
				if ((now.getDate()) < 10) {
					day = "0" + (now.getDate());
				} else {
					day = "" + (now.getDate());
				}
				// s页面上显示的时间筛选
				
				
				
				// var weekarray =[];
				if(this.tabcur==1){
					console.log("执行");
					if(endstart == 'start'){
						
						this.weektypeArray[0]=month + "-" + day;
					}
					if(endstart == 'end'){
						
						this.weektypeArray[1]=month + "-" + day;
					}
					this.monthweekshow = this.weektypeArray[0] + " "  + this.weektypeArray[1];
				}else{
					this.monthweekshow =year + "-" + month;
				}
				// 
				console.log('this.weekarray',this.weektypeArray);
			
				if (endstart == 'start') {
					return year + "-" + month + "-" + day + ' 00:00:00';
				} else {
					return year + "-" + month + "-" + day + ' 23:59:59';
				}

			},

			
			checkStatistics(timearray){
				var  _that = this;
				let daySart=timearray[0];
				let dayEnd=timearray[1];
				
				console.log(daySart);
				console.log(dayEnd);
				var data =  new Object();
				data.from= daySart;
				data.to = dayEnd;
				data.userId = this.userId;
				util.request(api.checkStatistics , data, "POST").then(res => {
					console.log("签到查询返回",res);
					if(res.error==0){
						var data = res.data;
						_that.checkInNum  =data.checkInNum;
						_that.checkInDays  =data.checkInDays;
						_that.unCheckInDays  =data.unCheckInDays;
						_that.openNums  =data.openNums;
						_that.unOpenNums  =data.unOpenNums;
						
						_that.userInfo  = data.userInfo;
						
					}else{
						_that.checkInNum  =0;
						_that.checkInDays  =0;
						_that.unCheckInDays  =0;
						_that.openNums  =0
						_that.unOpenNums  =0;
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
					
				})
				
			},
		}
	}
</script>

<style>
	@import url("./signinCalc.css");
</style>
